<?php
namespace Modules\ShopApi\Models;
use Illuminate\Support\Facades\DB;
use Modules\ShopApi\Services\ShopUserService;
use Modules\ShopApi\Models\ShopOrder as ShopOrderModel;
class ShopOrderEvaluate extends BaseApiModel
{
	/**
	 * @name  商品评价
	 * @return  JSON
	 */
	public function order_evaluate($data){
		$user_info = (new ShopUserService())->my();
		if(ShopOrderModel::where(['id'=>$data[0]['order_id'],'user_id'=>$user_info['id']])->value('status') != 4){
			return $this->api_error();
		};
		$arr = [];
		$date = date('Y-m-d H:i:s');
		foreach($data as $k=>$v){
			$arr[$k]['created_at'] = $date;
			$arr[$k]['order_id'] = $v['order_id'];
			$arr[$k]['product_id'] = $v['product_id'];
			$arr[$k]['user_id'] = $user_info['id'];
			$arr[$k]['level'] = $v['level'];
			if($v['content']){
				$arr[$k]['content'] = $v['content'];
			}
			$arr[$k]['images'] = [];
			if(count($v['images'])){
				foreach($v['images'] as $k1=>$v1){
					$arr[$k]['images'][] = $v1['image_id'];
				}
			}
			if(count($arr[$k]['images'])){
				$arr[$k]['images'] = implode('|',$arr[$k]['images']);
			}else{
				unset($arr[$k]['images']);
			}
		}
		DB::beginTransaction();
		try{
			$this->insert($arr);
			ShopOrderModel::where(['id'=>$arr[0]['order_id'],'user_id'=>$user_info['id']])->update([
				'status'=>9,
				'receive_time'=>$date
			]);
			DB::commit();
		}catch(\Exception $e){
			DB::rollBack();
			return $this->api_error();
		}
		return $this->api_success();
	}
}
